package src.数据结构.队列;

public class Queue<T> {
    /*
    * 队列：先进先出
    * */
    private Node head; // 头节点
    private Node end; // 尾节点
    private int N;

    public Queue() {
        this.head = null;
        this.end = null;
        this.N = 0;
    }

    public boolean isEmpty() {
        return this.N == 0;
    }

    public int size() {
        return this.N;
    }

    // 插入数据 头 -> 尾  1，2，3，4，5
    public void enqueue(T t){
        // 空插入
        if(this.end == null){
            this.end = new Node<>(t, null);
            this.head = this.end;
            this.head.next = this.end;
        }else{
            // 非空插入
            Node old = this.end;
            this.end = new Node<>(t, null);
            old.next = this.end;
        }
        this.N++;
    }

    // 取出数据 头 -> 尾  1，2，3，4，5
    public T dequeue(){
        if(isEmpty()){
            this.head = null;
            this.end = null;
            return null;
        }else {
            // 获取首节点
            Node<T> pop = this.head;
            this.head = this.head.next;
            this.N--;
            return pop.item;
        }
    }


    public void show() {
        Node<T> n = head;
        while (n != null) {
            System.out.print(n.item + " ");
            n = n.next;
        }
        System.out.println();
    }
}
